package com.proton.carepatchtemp.login.fragment;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.base.BaseFragment;
import com.proton.carepatchtemp.constant.Codes;
import com.proton.carepatchtemp.utils.eventbus.MainEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.Calendar;

import cn.qqtheme.framework.picker.DatePicker;


public class Guide1Fragment extends BaseFragment {
    public EditText mEt_account;
    private TextView mTxt_man;
    private TextView mTxt_woman;
    private TextView mBirthday;
    private View rootView;
    private TextView mTxt_account;
    private boolean isManChecked = true;
    private DatePicker mDatePicker;

    public static Guide1Fragment newInstance() {
        Guide1Fragment f = new Guide1Fragment();
        Bundle args = new Bundle();
        f.setArguments(args);
        return f;
    }

    @Override
    public void dealEvent(MainEvent event) {
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //  出生年月选择器
        mDatePicker = new DatePicker(getActivity(), DatePicker.YEAR_MONTH_DAY);
        mDatePicker.setRangeStart(1900, 1, 1);//开始范围
        mDatePicker.setRangeEnd(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH) + 1, Calendar.getInstance().get(Calendar.DAY_OF_MONTH) + 1);//结束范围
        mDatePicker.setSelectedItem(2000, 1, 1);
        mDatePicker.setTitleText(getString(R.string.birthday));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_guide1, null);
        }
        // 缓存的rootView需要判断是否已经被加过parent，如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。 
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        bindViews(rootView);
        onListeners();
        onInitSex();
        return rootView;
    }

    @Override
    public void onEventMainThread(MainEvent event) {
        dealOnEvent(event);
    }

    private void bindViews(View rootView) {
        mEt_account = rootView.findViewById(R.id.et_name);
        mTxt_man = rootView.findViewById(R.id.txt_man);
        mTxt_woman = rootView.findViewById(R.id.txt_woman);
        mBirthday = rootView.findViewById(R.id.tv_birthday);
        mTxt_account = rootView.findViewById(R.id.tv_name);
    }

    @Override
    public void onPause() {
        super.onPause();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    public void onListeners() {
        mEt_account.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    mTxt_account.setVisibility(View.INVISIBLE);
                } else {
                    mTxt_account.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                String string = mEt_account.getText().toString();
                EventBus.getDefault().post(new MainEvent(Codes.POST_NAME, string));
            }
        });

        mBirthday.setOnClickListener(view -> {
            mDatePicker.show();
        });

        //生日设置监听器
        mDatePicker.setOnDatePickListener((DatePicker.OnYearMonthDayPickListener) (year, month, day) -> {
            mBirthday.setText(year + " 年" + month + "月" + day + "日");
            String str = year + "-" + month + "-" + day;
            EventBus.getDefault().post(new MainEvent(Codes.POST_BIRTH, str));
        });


        mTxt_man.setOnClickListener(view -> {
            if (isManChecked) {
                mTxt_man.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.checked, 0);
                mTxt_woman.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.unchecked, 0);
            } else {
                mTxt_man.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.checked, 0);
                mTxt_woman.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.unchecked, 0);
                isManChecked = !isManChecked;
            }
            EventBus.getDefault().post(new MainEvent(Codes.POST_MAN));
        });


        mTxt_woman.setOnClickListener(view -> {
            if (isManChecked) {
                mTxt_woman.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.checked, 0);
                mTxt_man.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.unchecked, 0);
                isManChecked = !isManChecked;
            } else {
                mTxt_woman.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.checked, 0);
                mTxt_man.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.unchecked, 0);
            }
            EventBus.getDefault().post(new MainEvent(Codes.POST_WOMAN));
        });
    }

    public void onInitSex() {
        if (isManChecked) {
            mTxt_man.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.checked, 0);
        }
    }
}
